<?php
	require_once("Database.php");

	class Experience{
		private $id=0;
		private $curriculum=0;
		private $enterprise="";
		private $activity="";
		private $startDate="";
		private $endDate="";
		private $result="";
		
		public function __construct($curriculum, $enterprise, $activity, $start, $end){
			$this->curriculum = $curriculum;
			$this->enterprise = $enterprise;
			$this->activity = $activity;
			$this->startDate = $start;
			$this->endDate = $end;
		}
		
		public function setId($id){
			$this->id = $id;
		}
		
		public function setCurriculum($curriculumId){
			$this->curriculum = $curriculumId;
		}
		
		public function setEnterprise($enterprise){
			$this->enterprise = $enterprise;
		}
		
		public function setActivity($activity){
			$this->activity = $activity;
		}
		
		public function setStartDate($start){
			$this->startDate = $start;
		}
		
		public function setEndDate($end){
			$this->endDate = $end;
		}
		
		public function getId(){
			return $this->id;
		}
		
		public function getCurriculum(){
			return $this->curriculum;
		}
		
		public function getEnterprise(){
			return $this->enterprise;
		}
		
		public function getActivity(){
			return $this->activity;
		}
		
		public function getStartDate(){
			return $this->startDate;
		}
		
		public function getEndDate(){
			return $this->endDate;
		}
		
		public function findById($id){
			$db = new Database("localhost", "root", "");
			$db->connect("emprego");
			
			$db->executeQuery("SELECT * FROM experience WHERE id='$id'");
			if($line = $db->getResultLine(MYSQL_ASSOC)){
				$this->setId($id);
				$this->setCurriculum($line["curriculum"]);
				$this->setEnterprise($line["enterprise"]);
				$this->setActivity($line["activity"]);
				$this->setStartDate($line["start_date"]);
				$this->setEndDate($line["end_date"]);
				
				$db->disconnect();
				return TRUE;
			}else{
				$db->disconnect();
				return FALSE;
			}
		}
		
		public function findByCurriculum($curriculumId){
			if($this->result == ""){
				$db = new Database("localhost", "root", "");
				$db->connect("emprego");
				$db->executeQuery("SELECT * FROM experience WHERE curriculum='$curriculumId'");
				$this->result = $db->getResult();
				$db->disconnect();
			}
			if($line = mysql_fetch_array($this->result, MYSQL_ASSOC)){
				$this->setId($line["id"]);
				$this->setCurriculum($line["curriculum"]);
				$this->setEnterprise($line["enterprise"]);
				$this->setActivity($line["activity"]);
				$this->setStartDate($line["start_date"]);
				$this->setEndDate($line["end_date"]);
				return TRUE;
			}else{
				$this->result = "";
				return FALSE;
			}
		}
		
		public function save(){
			$db = new Database("localhost", "root", "");
			$db->connect("emprego");
			
			$db->executeQuery("SELECT * FROM experience WHERE id='$this->id'");
			if($line = $db->getResultLine(MYSQL_ASSOC)){
				$db->executeQuery("UPDATE experience SET curriculum='$this->curriculum', enterprise='$this->enterprise',
									activity='$this->activity', start_date='$this->startDate', end_date='$this->endDate' WHERE id='$this->id'");
				return 1;
			}else{
				$db->executeQuery("INSERT INTO experience(curriculum, enterprise, activity, start_date, end_date) VALUES(
									$this->curriculum, '$this->enterprise', '$this->activity', '$this->startDate', '$this->endDate')");
				return 2;
			}
		}
	}
?>